package com.wuliming.gulimall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wuliming.constant.ProductConstant;
import com.wuliming.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wuliming.gulimall.product.dao.AttrDao;
import com.wuliming.gulimall.product.dao.AttrGroupDao;
import com.wuliming.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wuliming.gulimall.product.entity.AttrEntity;
import com.wuliming.gulimall.product.entity.AttrGroupEntity;
import com.wuliming.gulimall.product.entity.vo.req.AttrGroupReqVo;
import com.wuliming.gulimall.product.entity.vo.resp.AttrGroupRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuliming.common.utils.PageUtils;
import com.wuliming.common.utils.Query;

import com.wuliming.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrDao attrDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<AttrGroupEntity>());

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)) {
            wrapper.and(w -> {
                w.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> longs = relationEntities.stream().map(w -> w.getAttrId()).collect(Collectors.toList());
        if (longs == null || longs.size() == 0) {
            return null;
        }
        return attrDao.selectBatchIds(longs);
    }

    @Override
    public void removeBatch(AttrGroupReqVo[] reqVos) {
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(reqVos).stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(item.getAttrGroupId());
            relationEntity.setAttrId(item.getAttrId());
            return relationEntity;
        }).collect(Collectors.toList());
        relationDao.removeBatch(entities);
    }

    @Override
    public PageUtils getNoAttrRelation(Long attrgroupId, Map<String, Object> params) {
        AttrGroupEntity groupEntity = getById(attrgroupId);
        List<AttrGroupEntity> attrEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", groupEntity.getCatelogId()));
        List<Long> groupIds = attrEntities.stream().map(w -> w.getAttrGroupId()).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao
                .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", groupIds));
        List<Long> attrId = relationEntities.stream().map(w -> w.getAttrId()).collect(Collectors.toList());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
                .eq("catelog_id", groupEntity.getCatelogId())
                .eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        if (attrId != null && attrId.size() > 0) {
            wrapper.notIn("attr_id", attrId);
        }
        IPage<AttrEntity> page = attrDao.selectPage(new Query<AttrEntity>().getPage(params)
                , wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupRespVo> getAttrGroupWithAttr(Long categoryId) {
        List<AttrGroupEntity> attrGroupEntityList = list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", categoryId));
        List<AttrGroupRespVo> attrGroupRespVos = attrGroupEntityList.stream().map(w -> {
            AttrGroupRespVo attrGroupRespVo = new AttrGroupRespVo();
            BeanUtils.copyProperties(w, attrGroupRespVo);
            List<AttrAttrgroupRelationEntity> relationEntities = relationDao
                    .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", w.getAttrGroupId()));
            List<Long> attrIds = relationEntities.stream().map(a -> a.getAttrId()).collect(Collectors.toList());
            if (attrIds != null && attrIds.size()>0) {
                List<AttrEntity> attrEntities = attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id", attrIds));
                attrGroupRespVo.setAttrs(attrEntities);
            }
            return attrGroupRespVo;
        }).collect(Collectors.toList());
        return attrGroupRespVos;
    }


}